package com.zcsy.manage.web.common;	

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jboss.resteasy.plugins.spring.SpringContextLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.BrowserUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.commons.util.IpUtil;
import com.zcsy.manage.model.system.Log;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.system.LogDao;
import com.zcsy.manage.web.common.aop.LogAspect;


/**
 * @Title: LogUtils.java
 * @Package： com.zcsy.shop.web.common 
 * @Description: 〈一句话功能简述〉
 *  〈功能详细描述〉
 *
 * @author caiyinli 13277030520@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年8月25日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */

public class LogUtils {
	
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
	
	private static LogDao logDao = SpringContextLoader.getCurrentWebApplicationContext().getBean(LogDao.class);

	/** 
	 * @Title: insertLog 
	 * @Description: 插入日志
	 * @param @param content     
	 * @return void     
	 * @throws 
	 */
	public static void insertLog(String content,String recordId,HttpServletRequest request){
        HttpSession session = request.getSession();  
        //读取session中的用户  
        User user = (User) session.getAttribute(RequestConstants.USER_SESSION_ID);  
        if(user!=null){
       	//请求的IP  
            String ip = IpUtil.getIp(request);   
             try {  
                logger.info("Name:"+user.getUsername()+"--IP:" + ip+" --do:" + content);  
                Log Log = new Log();
                Log.setLogcontent(content);
                Log.setOperatetime(DateUtils.getFmtDate());
                Log.setUserId(user.getId());
                Log.setBrowser(BrowserUtils.checkBrowse(request));
                Log.setIp(ip);
                Log.setRecordId(recordId);
                Log.setId(UniqueNumberUtils.get18UniqueNumber());
                logDao.insert(Log);
            }  catch (Exception e) {  
                logger.error("异常信息:{}", e);  
            }  
        }
	}
}
